/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package validation;

/**
 *
 * @author elfatah
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.UserBean;
import database.DBClass;
import javax.swing.JOptionPane;

public class LoginValidation {

    public String authenticateUser(UserBean loginBean) throws SQLException, ClassNotFoundException {

        String userName = loginBean.getUserName(); //Keeping user entered values in temporary variables.
        String password = loginBean.getPassword();
        //   String status = "SUCCESS";
        Connection con = null;
        Statement statement = null;
        ResultSet resultSet = null;

//        String userNameDB;
//        String passwordDB;
        //  try {
        con = DBClass.createConnection(); //establishing connection
        statement = con.createStatement();
        String sql = "select * from userdetails where userName= '" + userName + "' and password= md5('" + password + "rbplsalting#12345')"; //Statement is used to write queries. Read more about it.
        
        System.out.println(sql);
        resultSet = statement.executeQuery(sql); //Here table name is users and userName,password are columns. fetching all the records and storing in a resultSet.

        if (resultSet.next()) // Until next row is present otherwise it return false
        {
//                userNameDB = resultSet.getString("userName"); //fetch the values present in database
//                passwordDB = resultSet.getString("password");

            // if (userName.equals(userNameDB) && password.equals(passwordDB)) {
            return "SUCCESS"; ////If the user entered values are already present in database, which means user has already registered so I will return SUCCESS message.
            //}
        } else {
            return "LOGIN FAILED BRO!!!!!";
            
        }
        
        
        // Just returning appropriate message otherwise
        // } catch (SQLException e) {
        //   e.printStackTrace();
    }

}
